# -*- coding: utf-8 -*-
import os
import configparser
import sys

class ConfigIni():
    def __init__(self,path=None):
        self.current_directory = os.path.split(
            os.path.realpath(sys.argv[0]))[0]
        if not path:
            self.path =self.current_directory+"/config/test_info.ini"
        else:
            self.path=path
        # self.path = os.path.split(__file__)[0].replace('Common','config/test_info.ini')
        self.cf = configparser.ConfigParser()

        self.cf.read(self.path,encoding='utf-8')

    def get_ini(self, title, value):
        return self.cf.get(title, value)

    def set_ini(self, title, key, value):
        if not self.has_section(title):
            self.cf.add_section(title)
        self.cf.set(title, key, str(value))
        return self.cf.write(open(self.path, "w"))

    def add_ini(self, title):
        self.cf.add_section(title)
        return self.cf.write(open(self.path))

    def get_options(self, data):
        # 获取所有的section
        options = self.cf.options(data)
        return options

    def has_section(self,section):
        return self.cf.has_section(section)

    def remove_section(self,section):
        self.cf.remove_section(section)